
/**
 *
 *  utf-8编码 和4bit编码 互转
 *
 */
export default  class StrTo{




    /**
     *
     *  utf-8编码 转换成 十进制编码
     *  把buff（八比特编码）拆分转换成四比特编码
     * @param txtData
     * @returns {*}
     */
    static utf8ToNum(utf){
        let buff=new TextEncoder().encode(utf)
        let _this=this;
        let _str="";
        for (let i=0;i<buff.length;i++){
            let code= buff[i].toString(2);
            code= (Array(8).join(0) + code).slice(-8);
            _str=_str+code;
        }
        let thatList=_str.split("");
        let num=  _this._sum(thatList);
        /*   let arr=_this.numToCode(num);
           let resBuff=_this.toBuff(arr);
   */
        return num;
    }
 
    /**
     *
     * 二进制 按位转换 成十进制
     */
    static  _sum(a){
        let base ='1';
        let n=a.length;
        let sum="0";
        for(let i=0;i<n;n--){
            if(a[n-1]>0){
                sum=this.sumBigNumber(sum,base)
            }
            base=this.sumBigNumber(base,base);
        }
        return sum;
    }

    /**
     * 超大额数字相加
     * @param a
     * @param b
     * @returns {string}
     */
    static   sumBigNumber(a, b) {
        let res = '',
            temp = 0;
        a = a.split('');
        b = b.split('');
        while (a.length || b.length || temp) {
            temp += ~~a.pop() + ~~b.pop();
            res = (temp % 10) + res;
            temp = temp > 9;
        }
        return res.replace(/^0+/, '');
    }

    
    /**
     *
     * 十进制编码 转换成    utf-8编码
     *  把buff（八比特编码）拆分转换成四比特编码
     * @param txtData
     * @returns {*}
     */
    static numToUtf8(num){
        //let num= this.buffToNum(bit);
        let listNums=this._4bitToUtf(num);
        let tes1=new TextDecoder();
        let buff=new Uint8Array(listNums);
        return tes1.decode(buff);
    }
    static  _4bitToUtf(big){
        if(!big) return [];
        let bigs=big.split("");
        let ans=[];

        let sum = 1;//赋一个初值，开始循环
        let _more=0;
        while ((bigs.length-1)||bigs[0]!=0) {
            sum = 0;
            let _big = "";
            let _more = 0;
            let w=0;
            if(bigs[0]==0){
                w=1;
            }

            for (let i = w; i < bigs.length; ++i) {
                _big += parseInt(parseInt(_more + "" + bigs[i]) / 2).toString();//当num所有位都变成0，运算结束
                _more = parseInt(_more + "" + bigs[i]) % 2;
                if (i == bigs.length - 1) {//运算到最后一位，一次除法完毕，存余数
                    {

                        ans.push(parseInt(_more.toString()));
                        bigs = _big.split("");
                        _big = "";
                    }
                }
            }
        }

        let res1 = [];
        for (let i =0 ; i < ans.length;) {
            let text="";
            for(let l=0;l<8;l++,i++){
                if (ans[i]!==undefined){
                    text=ans[i]+text;
                }

            }
            text=(Array(8).join(0) + text).slice(-8)
            res1=[parseInt(text,2),...res1]
        }
        return res1
    }


    static  buffToNum(data){
        let buffData=  new Uint8Array(data)//
        let list=[];
        for(let i=0;i<buffData.length;i++){
            let code= buffData[i].toString(2);
            code= (Array(8).join(0) + code).slice(-8)
            list.push ( code.substr(0,4));
            list.push(  code.substr(4,7));
        }
        let nums="";
        let listNums=[]
        let listLength=list.length;
        for(let l=0;l<list.length;l++) {
            let numb = parseInt(list[l], 2);
            nums+=numb.toString();
        }
        return nums;
    }


    /**
     * 十进制，拆分，转换生二进制数组
     * @param data
     * @returns {Array}
     */
    static numToCode(data) {
        let list = data.toString().split("");
        let res = []
        for (let i = 0; i < list.length; i++) {
            let num = parseInt(list[i], 16).toString(2);
            res.push((Array(4).join(0) + num).slice(-4))
        }
        return res;
    }

    /**
     * 二进制数组 转换成 buff
     * @param dataList
     * @returns {Uint8Array}
     */
    static toBuff(dataList){
        let buffList=[];
        if(dataList.length%2==1){ //如果如果长度单数，则在最后补1
            dataList=["0000",...dataList]
        }

        for(let i=0;i<dataList.length;i=i+2){
            let text = dataList[i]+dataList[i+1];
            let intcode= parseInt(text,2)
            buffList.push( intcode)
        }
        return new Uint8Array(buffList);
    }

}

/*
let res=StrTo.utf8ToNum("中");
 console.log(res)
console.log(StrTo.numToUtf8(res))*/
